package as3feedparser.syndication.parsers
{
    import as3feedparser.syndication.model.FeedText;
    import as3feedparser.syndication.model.FeedTextType;
    import as3feedparser.syndication.model.atom.AtomCategory;
    import as3feedparser.syndication.model.atom.AtomEntry;
    import as3feedparser.syndication.model.atom.AtomFeed;
    import as3feedparser.syndication.model.atom.AtomPerson;

    import org.flexunit.Assert;

    public class TestAtomParser
    {
        [Before]
        public function setUp() : void
        {
            parser = new FeedParser();
        }

        [After]
        public function tearDown() : void
        {
            parser = null;
        }

        [Test]
        public function testParseAtomSimple() : void
        {
            var feed : AtomFeed = parser.parse(INLINE_ATOM_DOCUMENT_SIMPLE) as AtomFeed;
            Assert.assertTrue(feed != null);
            Assert.assertTrue(feed.title.text == "Example Feed");
            Assert.assertTrue(feed.link.url == "http://example.org/");
            Assert.assertTrue(feed.updated == "2003-12-13T18:30:02Z");
            Assert.assertTrue(feed.authors != null);
            Assert.assertTrue(feed.authors[0].name == "John Doe");
            Assert.assertTrue(feed.id == "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6");
            Assert.assertTrue(feed.entries != null);
            Assert.assertTrue(feed.entries.length == 1);

            var entry : AtomEntry = feed.entries[0] as AtomEntry;
            Assert.assertTrue(entry.title.text == "Atom-Powered Robots Run Amok");
            Assert.assertTrue(entry.link.url == "http://example.org/2003/12/13/atom03");
            Assert.assertTrue(entry.id == "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a");
            Assert.assertTrue(entry.updated == "2003-12-13T18:30:02Z");
            Assert.assertTrue(entry.description.text == "Some text.");
        }

        [Test]
        public function testParseAtom() : void
        {
            var feed : AtomFeed = parser.parse(INLINE_ATOM_DOCUMENT) as AtomFeed;
            Assert.assertTrue(feed != null);
            Assert.assertTrue(feed.subtitle.type == FeedTextType.HTML);
            Assert.assertTrue(feed.subtitle.text != "");
            Assert.assertTrue(feed.rights.text == "Copyright (c) 2003, Mark Pilgrim");

            var contributors : Vector.<AtomPerson> = feed.contributors;
            Assert.assertTrue(contributors != null);
            Assert.assertTrue(contributors.length == 1);

            var contributor : AtomPerson = contributors[0];
            Assert.assertTrue(contributor.name == "Jane Doe");
            Assert.assertTrue(contributor.url == "http://www.janedoe.com");

            var icon : String = feed.icon;
            var logo : String = feed.logo;
            Assert.assertTrue(icon == "/icon.jpg");
            Assert.assertTrue(logo == "/logo.jpg");

            var categories : Vector.<AtomCategory> = feed.categories;
            Assert.assertTrue(categories != null);
            Assert.assertTrue(categories.length == 1);

            var category : AtomCategory = categories[0];
            Assert.assertTrue(category != null);
            Assert.assertTrue(category.term == "sports");

            var entry : AtomEntry = feed.entries[0] as AtomEntry;
            Assert.assertTrue(entry != null);
            Assert.assertTrue(entry.published == "2003-12-13T08:29:29-04:00");

            categories = entry.categories;
            Assert.assertTrue(categories != null);
            Assert.assertTrue(categories.length == 1);

            category = categories[0];
            Assert.assertTrue(category != null);
            Assert.assertTrue(category.term == "technology");
            Assert.assertTrue(category.scheme == "scheme.category.tech");
            Assert.assertTrue(category.label == "technology");

            var source : AtomFeed = entry.source;
            Assert.assertTrue(source.id == "http://example.org/");
            Assert.assertTrue(source.title.text == "Fourty-Two");
            Assert.assertTrue(source.updated == "2003-12-13T18:30:02Z");
            Assert.assertTrue(source.rights.text == "© 2005 Example, Inc.");

            var rights : FeedText = entry.rights;
            Assert.assertTrue(rights.type == FeedTextType.HTML);

            var test : String = TestFeedParser.htmlUnescape("&amp;copy; 2005 John Doe");
            Assert.assertTrue(rights.text == test);
        }

        private static const INLINE_ATOM_DOCUMENT_SIMPLE : XML = <feed xmlns="http://www.w3.org/2005/Atom">
			  <title>Example Feed</title>
			  <link href="http://example.org/"></link>
			  <updated>2003-12-13T18:30:02Z</updated>
			  <author>
			    <name>John Doe</name>
			  </author>
			  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
			  <entry>
			    <title>Atom-Powered Robots Run Amok</title>
			    <link href="http://example.org/2003/12/13/atom03"/>
			    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
			    <updated>2003-12-13T18:30:02Z</updated>
			    <summary>Some text.</summary>
			  </entry>
			</feed>;

        private static const INLINE_ATOM_DOCUMENT : XML = <feed xmlns="http://www.w3.org/2005/Atom">
		     <title type="text">dive into mark</title>
		     <subtitle type="html">
		       A &lt;em&gt;lot&lt;/em&gt; of effort
		       went into making this effortless
		     </subtitle>
		     <updated>2005-07-31T12:29:29Z</updated>
		     <id>tag:example.org,2003:3</id>
			<contributor>
			  <name>Jane Doe</name>
			  <uri>http://www.janedoe.com</uri>
			</contributor>		     
		     <link rel="alternate" type="text/html"
        hreflang="en" href="http://example.org/"/>
		     <link rel="self" type="application/atom+xml"
        href="http://example.org/feed.atom"/>
		     <rights>Copyright (c) 2003, Mark Pilgrim</rights>
		     <generator uri="http://www.example.com/" version="1.0">
		       Example Toolkit
		     </generator>
			<icon>/icon.jpg</icon>
			<logo>/logo.jpg</logo>
			<category term="sports"/>					     
		     <entry>
		       <title>Atom draft-07 snapshot</title>
		       <link rel="alternate" type="text/html"
        href="http://example.org/2005/04/02/atom"/>
		       <link rel="enclosure" type="audio/mpeg" length="1337"
        href="http://example.org/audio/ph34r_my_podcast.mp3"/>
		       <id>tag:example.org,2003:3.2397</id>
		       <updated>2005-07-31T12:29:29Z</updated>
		       <published>2003-12-13T08:29:29-04:00</published>
				<rights type="html">
				  &amp;copy; 2005 John Doe
				</rights>		       
			   <category term="technology" scheme="scheme.category.tech" label="technology"/>
				<source>
				  <id>http://example.org/</id>
				  <title>Fourty-Two</title>
				  <updated>2003-12-13T18:30:02Z</updated>
				  <rights>© 2005 Example, Inc.</rights>
				</source>			          
		       <author>
		         <name>Mark Pilgrim</name>
		         <uri>http://example.org/</uri>
		         <email>f8dy@example.com</email>
		       </author>
		       <contributor>
		         <name>Sam Ruby</name>
		       </contributor>
		       <contributor>
		         <name>Joe Gregorio</name>
		       </contributor>
		       <content type="xhtml" xml:lang="en"
        xml:base="http://diveintomark.org/">
		         <div xmlns="http://www.w3.org/1999/xhtml">
		           <p><i>[Update: The Atom draft is finished.]</i></p>
		         </div>
		       </content>
		     </entry>
		   </feed>;

        private var parser : FeedParser;
    }
}
